﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HW3
{
    class ReadFile
    {
        private string filePath;
        
        private Prop prop;

        public string FilePath
        {
            get { return filePath; }
            set { filePath = value; }
        }

        public ReadFile(string file)
        {
            prop = new Prop();
            FilePath = file;
        }

        private string line;

        public Prop setProp()
        {
            if (File.Exists(filePath))
            {
                StreamReader file = null;
                try
                {
                    file = new StreamReader(filePath);
                    while ((line = file.ReadLine()) != null)
                    {
                        addProp(line);
                    }
                }
                finally
                {
                    if (file != null)
                        file.Close();
                }
            }
            return prop;
        }
        
        private void addProp(String line)
        {
            string[] words = line.Split(':');
            switch (words[0])
            {
                case "table1":
                    break;
                case "table2":
                    break;
                case "order size":
                    prop.OrderSize = spliteByComma(words[1]);
                    break;
                case "order price":
                    prop.OrderPrice = int.Parse(words[1]);
                    break;
                case "store price":
                    prop.StorePrice = int.Parse(words[1]);
                    break;
                case "lost price":
                    prop.LostPrice = int.Parse(words[1]);
                    break;
                case "initial amount":
                    prop.InitialAmount = int.Parse(words[1]);
                    break;
                case "timelength":
                    prop.TimeLength = int.Parse(words[1]);
                    break;
                case "quantity simulation":
                    prop.QuantitySimulation = int.Parse(words[1]);
                    prop.DailyOutcome = new double[int.Parse(words[1])];
                    break;
                default:
                    updateTables(words[0]);
                    break;
            }
        }

        private void updateTables(string words)
        {if(prop.Table1 == null){
            prop.Table1 = getTable1(words);
        }
        else {
            prop.Table2 = getTable2(words);
        }
        }

        private double[] getTable2(string line)
        {
            line = removeBrackets(line);
            double[] ans = spliteByCommaDouble(line);
            return ans;
        }

        private string removeBrackets(string line)
        {
            line = line.Replace(";", ",");
            line = line.Replace("(", "");
            line = line.Replace(")", "");
            return line;
        }

        private int[] spliteByDoubleComma(string line)
        {
            string[] words = line.Split(',');
            int[] ret = new int[words.Length-1]; 
            for (int i = 0; i < words.Length-1; i++)
            {
                ret[i] = int.Parse(words[i]);
            }
            
            return ret;
        }

        private int[] getTable1(string line)
        {
            line = removeBrackets(line);
            int[] ans = spliteByDoubleComma(line);
            return ans;
        }

        private int [] spliteByComma(String line)
        {
            string[] words = line.Split(',');
            int[] temp = new int[words.Length];
            for(int i =0;i<words.Length;i++)
            {
                temp[i] = int.Parse(words[i]);
            }
           return temp;
        }
        private double [] spliteByCommaDouble(String line)
        {
            string[] words = line.Split(',');
            double [] temp = new double[words.Length-1];
            for (int i = 0; i < words.Length-1; i++)
            {
                temp[i] = double.Parse(words[i]);
            }
            return temp;
        }
    }
}
